﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace XOAuth
{
    class Helper
    {

        public static string CurrentPath()
        {
            return Environment.CurrentDirectory;
        }

        public static string NewId()
        {
            return Guid.NewGuid().ToString("N");
        }

        public static string HEX(byte[] b, bool lowerCase)
        {
            var s = BitConverter.ToString(b).Replace("-", String.Empty);
            if (lowerCase) s = s.ToLower();
            return s;
        }
        public static string GetHashString(HashAlgorithm hash, byte[] content)
        {
            var h = hash.ComputeHash(content);
            return HEX(h, true);
        }
        public static string GetHashString(HashAlgorithm hash, Stream content)
        {
            var h = hash.ComputeHash(content);
            return HEX(h, true);
        }

        public static string Md5(string s, Encoding enc)
        {
            return GetHashString(MD5.Create(), enc.GetBytes(s));
        }
        public static string Md5(string s)
        {
            return Md5(s, Encoding.UTF8);
        }
        public static string Md5(Stream s)
        {
            return GetHashString(MD5.Create(), s);
        }

        public static string Sha1(string s, Encoding enc)
        {
            return GetHashString(SHA1.Create(), enc.GetBytes(s));
        }
        public static string Sha1(string s)
        {
            return Sha1(s, Encoding.UTF8);
        }
        public static string Sha1(Stream s)
        {
            return GetHashString(SHA1.Create(), s);
        }

        public static string Sha256(string s, Encoding enc)
        {
            return GetHashString(SHA256.Create(), enc.GetBytes(s));
        }
        public static string Sha256(string s)
        {
            return Sha256(s, Encoding.UTF8);
        }
        public static string Sha256(Stream s)
        {
            return GetHashString(SHA256.Create(), s);
        }

        public static string Sha384(string s, Encoding enc)
        {
            return GetHashString(SHA384.Create(), enc.GetBytes(s));
        }
        public static string Sha384(string s)
        {
            return Sha384(s, Encoding.UTF8);
        }
        public static string Sha384(Stream s)
        {
            return GetHashString(SHA384.Create(), s);
        }

        public static string Sha512(string s, Encoding enc)
        {
            return GetHashString(SHA512.Create(), enc.GetBytes(s));
        }
        public static string Sha512(string s)
        {
            return Sha512(s, Encoding.UTF8);
        }
        public static string Sha512(Stream s)
        {
            return GetHashString(SHA512.Create(), s);
        }

        public static RSA Rsa(string prefix)
        {
            return RSA.Create(prefix);
        }

        public static byte[] GetHexBytes(string hex)
        {
            if (String.IsNullOrEmpty(hex)) return new byte[0];
            int i = 0, l = hex.Length / 2;
            var result = new byte[l];
            for(; i < l; i++)
            {
                result[i] = byte.Parse(hex.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
            }
            return result;
        }

    }
}
